<form class="layui-form seller-form"  action="" >

    <div class="layui-form-item"> 
        <div class="layui-inline">
            <label class="layui-form-label seller-inline-2">部/院：</label>
            <div class="layui-input-inline" id="dep_box">
                <select name="dep_id" lay-filter="dep_id" id="dep_id" list="dep_list" lay-search>
                    <option value=""></option>
                    <datalist  id="dep_list" lay-search>
                        {volist name="deptList" id="deptList"}
                        <option value="{$deptList.bmdm}">{$deptList.bmmc}</option>
                        {/volist}
                    </datalist>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label seller-inline-2">科教中心：</label>
            <div class="layui-input-inline">
                <input type="text" name="sub_center_name" lay-filter="sub_center_name" id="sub_center_name" placeholder="请输入名称关键字" autocomplete="off" class="layui-input">
            </div>
        </div>
        <!-- <div class="layui-inline">
            <label class="layui-form-label seller-inline-2">科教中心：</label>
            <div class="layui-input-inline" id="specialty_box">
                <select name="sub_center_id" lay-filter="sub_center_id" id="sub_center_id" lay-search>
                </select>
            </div>
        </div> -->
        <div class="layui-inline">
            <label class="layui-form-label seller-inline-2">专业：</label>
            <div class="layui-input-inline" id="specialty_box">
                <select name="specialty_id" lay-filter="specialty_id" id="specialty_id" lay-search>
                    <option value=""></option>
                </select>
            </div>
        </div>
 
        <div class="layui-inline">
            <button class="layui-btn layui-btn-sm" lay-submit lay-filter="subcenter-search"><i class="iconfont icon-chaxun"></i>筛选</button>
            <button class="layui-btn layui-btn-sm" lay-submit  lay-filter="subcenter-add"><i class="layui-icon">&#xe608;</i>添加</button>
            <button id="exportBtn" class="layui-btn layui-btn-sm layui-btn-disabled" lay-submit lay-filter="export-subcenter" disabled><i class="iconfont icon-clouddownload"></i>导出</button> 
        </div>

    </div>

</form>

<div class="table-body">
    <table id="subcenterTable" lay-filter="subcenterTable"></table>
</div>

<div id="exportSubCenter" style="display: none;">
    <form class="layui-form export-form" action="">
        <div class="layui-form-item">
            <div class="layui-margin-10">
                <blockquote class="layui-elem-quote layui-text">
                    请先筛选要导出的数据，默认导出全部
                </blockquote>
            </div>

            <div class="layui-inline">
                <label class="layui-form-label">任务名称：</label>
                <input type="text" name="taskname" lay-verify="title" style="width:200px;" placeholder="请输入任务名称"
                    autocomplete="off" class="layui-input">
            </div>
        </div>
    </form>
</div>

<script>
    layui.use(['table','form','layer','laydate'],function(){
        var layer = layui.layer, table = layui.table,form = layui.form,date = layui.laydate;
        // 执行渲染
        table.render({
            elem: '#subcenterTable', //指定原始表格元素选择器（推荐id选择器）
            height: 'full-115',
            cellMinWidth: '80',
            page: 'true',
            limit:'20',
            id:'subcenterTable',
            url: "{:url('BookDraw/subcenter')}",
            cols: [[ //标题栏
                {type:'numbers'},
                {field: 'subcenter_id', title: 'ID', hide:true},
                {field: 'deptName', title: '部/院', width:200, align:'center'}, 
                {field: 'centerName', title: '科教中心', width:200, align:'center'}, 
                {field: 'specialtyName', title: '专业', width:200, align:'center'},
                {field: 'operating', title: '操作', width:200, align: 'center',templet:function(data){
                    var html = '';
                    html += '<a  class="layui-btn layui-btn-xs option-edit" lay-event="edit" data-id="' + data.id + '">编辑</a>';
                    html += '<a  class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del" data-id="' + data.id + '">删除</a>';
                    return html;
                }}
            ]], //设置表头
            done: function(res, curr, count){
                //得到数据总量
                totalCount = count;

                // 判断加载结果数量是否大于0
                if(count>0){
                    $("#exportBtn").removeClass("layui-btn-disabled");
                    $('#exportBtn').removeAttr("disabled");
                } else {
                    $('#exportBtn').addClass("layui-btn-disabled");
                    $('#exportBtn').attr("disabled", true);
                }
            }
            //,…… //更多参数参考右侧目录：基本参数选项
        });

        date.render({
            elem: '#utime',
            range: '到'
        });

        // 查询
        form.on('submit(subcenter-search)', function(data){
            layui.table.reload('subcenterTable', {
                where: data.field
                ,page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        //科教中心增加
        layui.form.on('submit(subcenter-add)', function (data) {
            var topLayui = parent === self ? parent.window.layui : top.parent.window.layui;
            topLayui.index.openTabsPage('{:url("BookDraw/subcenteradd")}', '科教中心增加'); 
            return false;
        }); 

        // 监听工具条
        table.on('tool(subcenterTable)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var id = data.subcenter_id;
            var deptName = data.deptName;
            var centerName = data.centerName;
            var specialtyName = data.specialtyName;

            if(layEvent === 'edit'){ // 修改
                var topLayui = parent === self ? parent.window.layui : top.parent.window.layui;
                var url = '{:url("BookDraw/subcenteredit")}?id=' + id + '&deptName=' + deptName + '&centerName=' + centerName + '&specialtyName=' + specialtyName;
                topLayui.index.openTabsPage(url, '科教中心编辑'); 
            }
            if(layEvent === 'del'){ // 删除
                layer.confirm('您确定删除此条记录吗？', {btn: ['确认', '取消'], }, function () {
                    JsPost("{:url('BookDraw/subcenterdel')}", {
                            "id": id,
                            "deptName": deptName,
                            "centerName": centerName,
                            "specialtyName": specialtyName,
                        }, function (res) {
                        if(res.status){
                            layui.table.reload('subcenterTable', {
                                // where: data.field, 
                                page: {curr: 1},
                                url: "{:url('BookDraw/subcenter')}",
                            });
                        } else {
                            layer.msg(res.msg);
                        }
                    });
                })
            }
        });

        layui.use('form', function () {
        // 联动部门
            form.on('select(dep_id)', function (data) {
                getSpecialty(data);
            });
        });
 
        // 得到专业
        function getSpecialty(data) {
            $.ajax({
                type: 'get',
                url: "{:url('BookCommon/getSpecicalties')}",
                data: 'bmdm=' + data.value,
                success: function(e) {
                    if(e.status) {
                        $("#specialty_id").empty();//清空下拉框的值
                        $('#specialty_id').append(new Option('', ''));// 下拉菜单里添加元素
                        
                        $("#class_id").empty();//清空下拉框的值
                        $('#class_id').append(new Option('', ''));// 下拉菜单里添加元素
                        $.each(e.data.data, function (index, item) {
                            $('#specialty_id').append(new Option(item.zymc, item.zydm));// 下拉菜单里添加元素
                        });
                        layui.form.render("select");//重新渲染 固定写法
                    } else {
                        layer.msg(e.msg);
                    }
                }
            });
        };

        // 导出
        layui.form.on('submit(export-subcenter)', function (data) {
            if(totalCount == 0){
                layer.msg('没有数据，请查询出数据后再进行导出操作！！！');
                return false;
            }
            else{
                   layer.open({
                   type: 1,
                   title: '科教中心导出',
                   area: ['400px', '248px'], //宽高
                   btn: ['确定', '取消'],
                   content: $("#exportSubCenter").html(),
                   success: function () {
                       $(':focus').blur();
                       this.enterEsc = function(event){
                         if(event.keyCode === 13){
                             if(!$(".layui-layer-btn0").hasClass("layui-btn-disabled")){
                                 $(".layui-layer-btn0").addClass("layui-btn-disabled");

                                 //判断是否有选中
                                 var filter = $(".seller-form").serialize();
                                 $(".export-form:last").append("<input type='hidden' name='filter' value='" + filter + "'>");
                                 var data = $(".export-form:last").serializeArray();
                                 data.push({ 'name': 'model', 'value': 'SubCenter' });

                                 if($.trim(data[0]['value']) == '') {
                                     layer.msg('请填写任务名称！', { time: 1300 });
                                     $(".layui-layer-btn0").removeClass("layui-btn-disabled");
                                     return false;
                                 }

                                 JsPost("{:url('Ietask/export')}", data, function (res) {
                                         layer.msg(res.msg, { time: 1500 }, function () {
                                             if (res.status) {
                                                layer.closeAll();
                                                //执行跳转
                                                var topLayui = parent === self ? layui : top.layui;
                                                topLayui.index.openTabsPage('/manage/ietask/index.html', '导入导出');
                                             } else {
                                                $(".layui-layer-btn0").removeClass("layui-btn-disabled");
                                             }
                                         });
                                     }
                                 );
                             }
                             return false; //阻止系统默认回车事件
                         }
                     };
                     $(document).on('keydown', this.enterEsc);	//监听键盘事件，关闭层
                 },
                 yes: function () {
                     if(!$(".layui-layer-btn0").hasClass("layui-btn-disabled")){
                         $(".layui-layer-btn0").addClass("layui-btn-disabled");

                         //判断是否有选中
                         var filter = $(".seller-form").serialize();
                         $(".export-form:last").append("<input type='hidden' name='filter' value='" + filter + "'>");
                         var data = $(".export-form:last").serializeArray();
                        
                         if($.trim(data[0]['value']) == '') {
                             layer.msg('请填写任务名称！', { time: 1300 });
                             $(".layui-layer-btn0").removeClass("layui-btn-disabled");
                             return false;
                         }

                         data.push({ 'name': 'model', 'value': 'SubCenter' });
                         JsPost("{:url('Ietask/export')}", data, function (res) {
                                 layer.msg(res.msg, { time: 1500 }, function () {
                                     if (res.status) {
                                        layer.closeAll();
                                        //执行跳转
                                        var topLayui = parent === self ? layui : top.layui;
                                        topLayui.index.openTabsPage('/manage/ietask/index.html', '导入导出');
                                     } else {
                                        $(".layui-layer-btn0").removeClass("layui-btn-disabled");
                                     }
                                 });
                             }
                         );
                     }
                 }, btn2: function () {
                     layer.closeAll();
                 }, end: function(){
                     $(document).off('keydown', this.enterEsc);	        //解除键盘关闭事件
                 }
             });
             return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
         }
            
        });
    })
</script>